package com.zlzxm.studentscreen.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;
import android.view.View;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.zlzxm.studentscreen.R;
import com.zlzxm.studentscreen.net.websocket.message.PlayContent;
import com.zlzxm.studentscreen.service.CoreService;
import com.zlzxm.studentscreen.ui.activity.manager.ActivityStarter;
import com.zlzxm.studentscreen.ui.activity.manager.BaseActivity;
import com.zlzxm.studentscreen.ui.ijkplayer.RtspView;

/**
 * CREATE BY zlzxm ON 2022/3/9
 */
public class AcPlayer extends BaseActivity {

    public static String KEY = "URL";

    RtspView rtspView;

    private String url;

    private LocalBroadcastManager localBroadcastManager;

    private BroadcastReceiver myMessageBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            if(intent.getAction().equals(CoreService.CLOSE_SCREEN)){

                //展示验证码
                finish();

            }else   if(intent.getAction().equals(CoreService.PLAYER_SCREEN)){
                //展示验证码
                PlayContent playContent = (PlayContent) intent.getSerializableExtra(CoreService.PLAYER_SCREEN);
                if(!TextUtils.isEmpty(playContent.getUrl())){
                    rtspView.start(url);
                }

            }
        }
    };



    @Override
    public int loadRootView() {
        localBroadcastManager =  LocalBroadcastManager.getInstance(this);
        return R.layout.ac_player;
    }

    @Override
    public void initView() {
        rtspView = findViewById(R.id.rtspView);
    }

    @Override
    public void initClickListener() {

    }

    @Override
    protected void onResume() {
        super.onResume();

        IntentFilter messageFilter = new IntentFilter();
        messageFilter.addAction(CoreService.CLOSE_SCREEN);
        messageFilter.addAction(CoreService.PLAYER_SCREEN);
        localBroadcastManager.registerReceiver(myMessageBroadcastReceiver,messageFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        localBroadcastManager.unregisterReceiver(myMessageBroadcastReceiver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(rtspView!=null){
            rtspView.release();
        }
    }

    @Override
    public void initData() {
        url = getIntent().getStringExtra(KEY);
        if(!TextUtils.isEmpty(url)){
            rtspView.start(url);
        }
    }
}
